
package com.english.dao.hibernate;

import java.util.List;

import com.english.dao.hibernate.BaseDaoHibernate;
import com.english.model.English;
import com.english.dao.EnglishDao;

import org.springframework.orm.ObjectRetrievalFailureException;

public class EnglishDaoHibernate extends BaseDaoHibernate implements EnglishDao {

    /**
     * @see com.english.dao.EnglishDao#getEnglishs(com.english.model.English)
     */
    public List getEnglishs(final English english) {
        return getHibernateTemplate().find("from English");

        /* Remove the line above and uncomment this code block if you want 
           to use Hibernate's Query by Example API.
        if (english == null) {
            return getHibernateTemplate().find("from English");
        } else {
            // filter on properties set in the english
            HibernateCallback callback = new HibernateCallback() {
                public Object doInHibernate(Session session) throws HibernateException {
                    Example ex = Example.create(english).ignoreCase().enableLike(MatchMode.ANYWHERE);
                    return session.createCriteria(English.class).add(ex).list();
                }
            };
            return (List) getHibernateTemplate().execute(callback);
        }*/
    }

    /**
     * @see com.english.dao.EnglishDao#getEnglish(Long id)
     */
    public English getEnglish(final Long id) {
        English english = (English) getHibernateTemplate().get(English.class, id);
        if (english == null) {
            log.warn("uh oh, english with id '" + id + "' not found...");
            throw new ObjectRetrievalFailureException(English.class, id);
        }

        return english;
    }

    /**
     * @see com.english.dao.EnglishDao#saveEnglish(English english)
     */    
    public void saveEnglish(final English english) {
        getHibernateTemplate().saveOrUpdate(english);
    }

    /**
     * @see com.english.dao.EnglishDao#removeEnglish(Long id)
     */
    public void removeEnglish(final Long id) {
        getHibernateTemplate().delete(getEnglish(id));
    }
}
